package com.example.restjpa.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.restjpa.entity.Customer;
import com.example.restjpa.repository.CustomerRepository;

@RestController
public class CustomerController {
	
	@Autowired
	CustomerRepository customerRepository;
	
	@RequestMapping(value="/customer",method=RequestMethod.POST)
	public Customer addCusotmer(@RequestBody Customer customer) {
		return customerRepository.save(customer);
	}
	
	@RequestMapping(value="/customer",method=RequestMethod.GET)
	public List<Customer> getAllCustomers(){
		return customerRepository.findAll();
	}
	
	@RequestMapping(value="/customer/{id1}",method=RequestMethod.GET)
	public Customer getCustomer(@PathVariable(name="id1") int id){
		return customerRepository.findOne(id);
	}
	
	@RequestMapping(value="/customer/{id1}",method=RequestMethod.DELETE)
	public void deleteCustomer(@PathVariable(name="id1") int id){
		 customerRepository.delete(id);
	}
	
	@RequestMapping(value="/customer",method=RequestMethod.PUT)
	public Customer updateCustomer(@RequestBody Customer customer){
		return customerRepository.save(customer);
	}
}
